package webmarket.controller.transferobjects;

import java.math.BigInteger;
import java.util.LinkedList;
import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

import org.apache.logging.log4j.LogManager;

import webmarket.common.CommonMethods;
import webmarket.common.XmlBase;
import webmarket.model.ReadLog;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class LogTO extends XmlBase {
	private static final long serialVersionUID = 2084772746583577296L;

	public LogTO() {

	}

	private int id;
	private String marker;
	private String message;
	private String username;
	private BigInteger millis;

	public LogTO(ReadLog log) {
		LogManager.getLogger().entry(log.getId());
		this.setId(log.getId());
		try {
			this.setMarker(CommonMethods.printMarker(log.getMarker()));
		} catch (Exception e) {
			this.setMarker(log.getMarker().getName());
		}
		this.setMessage(log.getMessage().getFormattedMessage());
		if (log.getUser() != null) {
			this.setUsername(log.getUser().getUsername());
		} else {
			this.setUsername("");
		}
		this.setMillis(log.getMillis());
		LogManager.getLogger().exit(log.getId());
	}

	public static List<LogTO> convert(List<ReadLog> from) {
		List<LogTO> result = new LinkedList<LogTO>();
		for (ReadLog log : from) {
			result.add(new LogTO(log));
		}
		return result;
	}

	/**
	 * @return the marker
	 */
	public String getMarker() {
		return marker;
	}

	/**
	 * @param marker
	 *            the marker to set
	 */
	public void setMarker(String marker) {
		this.marker = marker;
	}

	/**
	 * @return the message
	 */
	public String getMessage() {
		return message;
	}

	/**
	 * @param message
	 *            the message to set
	 */
	public void setMessage(String message) {
		this.message = message;
	}

	/**
	 * @return the username
	 */
	public String getUsername() {
		return username;
	}

	/**
	 * @param username
	 *            the username to set
	 */
	public void setUsername(String username) {
		this.username = username;
	}

	/**
	 * @return the millis
	 */
	public BigInteger getMillis() {
		return millis;
	}

	/**
	 * @param millis
	 *            the millis to set
	 */
	public void setMillis(BigInteger millis) {
		this.millis = millis;
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "ReadLog, id: " + getId();
	}

	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}
}
